package com.cloudbroker.bcs.platform.dav.middle.bizservice.impl;

import java.math.BigDecimal;
import java.util.*;

import javax.annotation.Resource;

import com.cloudbroker.bcs.common.util.*;
import com.cloudbroker.bcs.common.util.StringUtil;
import com.cloudbroker.bcs.csvc.uc.api.vo.request.LTBRedirectLoginTokenForm;
import com.cloudbroker.bcs.platform.dav.middle.util.*;
import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cloudbroker.bcs.common.configure.ConfigUtil;
import com.cloudbroker.bcs.common.entity.HSBCPSBackVO;
import com.cloudbroker.bcs.csvc.uc.api.constants.LTBInfo;
import com.cloudbroker.bcs.csvc.uc.api.vo.request.FuncAdviserForm;
import com.cloudbroker.bcs.csvc.uc.api.vo.request.FuncLTBTokenForm;
import com.cloudbroker.bcs.csvc.uc.api.vo.response.AdviserInfoVo;
import com.cloudbroker.bcs.csvc.uc.api.vo.response.LTBDynamicList;
import com.cloudbroker.bcs.csvc.uc.api.vo.response.LTBTokenBackVo;
import com.cloudbroker.bcs.platform.dav.bizapi.constants.HSBCPSErrorCodes;
import com.cloudbroker.bcs.platform.dav.bizapi.constants.HSBCPSErrorCodes.ClientErr;
import com.cloudbroker.bcs.platform.dav.bizapi.constants.HSBCPSErrorCodes.QuestionErr;
import com.cloudbroker.bcs.platform.dav.bizapi.constants.HSBCPSErrorCodes.SysMgtErr;
import com.cloudbroker.bcs.platform.dav.bizapi.constants.status.DynamicType;
import com.cloudbroker.bcs.platform.dav.bizapi.constants.status.PaySource;
import com.cloudbroker.bcs.platform.dav.bizapi.constants.status.UpvoteStatus;
import com.cloudbroker.bcs.platform.dav.bizapi.constants.type.ReplyType;
import com.cloudbroker.bcs.platform.dav.bizapi.service.base.vo.HSBCPSVO;
import com.cloudbroker.bcs.platform.dav.bizapi.service.base.vo.ListQueryVO;
import com.cloudbroker.bcs.platform.dav.bizapi.service.business.system.back.SelfViewpointBack;
import com.cloudbroker.bcs.platform.dav.bizapi.service.business.system.back.SelfViewpointList;
import com.cloudbroker.bcs.platform.dav.bizapi.service.business.system.back.SelfViewpointResult;
import com.cloudbroker.bcs.platform.dav.bizapi.service.business.system.vo.SelfViewpointByIdForm;
import com.cloudbroker.bcs.platform.dav.bizapi.service.business.system.vo.SelfViewpointForm;
import com.cloudbroker.bcs.platform.dav.bizapi.service.business.system.vo.SelfViewpointReviewForm;
import com.cloudbroker.bcs.platform.dav.bizapi.service.business.system.vo.SelfViewpointStatusForm;
import com.cloudbroker.bcs.platform.dav.bizapi.service.viewpoint.ViewPointBizService;
import com.cloudbroker.bcs.platform.dav.bizapi.service.viewpoint.result.CommentSaveResult;
import com.cloudbroker.bcs.platform.dav.bizapi.service.viewpoint.result.DynamicListData;
import com.cloudbroker.bcs.platform.dav.bizapi.service.viewpoint.result.DynamicListResult;
import com.cloudbroker.bcs.platform.dav.bizapi.service.viewpoint.result.LTBRealtimeQryData;
import com.cloudbroker.bcs.platform.dav.bizapi.service.viewpoint.result.LTBRealtimeQryResult;
import com.cloudbroker.bcs.platform.dav.bizapi.service.viewpoint.result.ReplyListData;
import com.cloudbroker.bcs.platform.dav.bizapi.service.viewpoint.result.ViewpointCommentResult;
import com.cloudbroker.bcs.platform.dav.bizapi.service.viewpoint.result.ViewpointContentData;
import com.cloudbroker.bcs.platform.dav.bizapi.service.viewpoint.result.ViewpointFansList;
import com.cloudbroker.bcs.platform.dav.bizapi.service.viewpoint.result.ViewpointNoLoginList;
import com.cloudbroker.bcs.platform.dav.bizapi.service.viewpoint.result.ViewpointReplyData;
import com.cloudbroker.bcs.platform.dav.bizapi.service.viewpoint.result.ViewpointSpitslotUpvoteResult;
import com.cloudbroker.bcs.platform.dav.bizapi.service.viewpoint.vo.DynamicListForm;
import com.cloudbroker.bcs.platform.dav.bizapi.service.viewpoint.vo.LTBRealtimeQryListForm;
import com.cloudbroker.bcs.platform.dav.bizapi.service.viewpoint.vo.ReplyListForm;
import com.cloudbroker.bcs.platform.dav.bizapi.service.viewpoint.vo.TenantKeyListQryForm;
import com.cloudbroker.bcs.platform.dav.bizapi.service.viewpoint.vo.ViewPointIdForm;
import com.cloudbroker.bcs.platform.dav.bizapi.service.viewpoint.vo.ViewpointCommentForm;
import com.cloudbroker.bcs.platform.dav.bizapi.service.viewpoint.vo.ViewpointCommentSaveForm;
import com.cloudbroker.bcs.platform.dav.bizapi.service.viewpoint.vo.ViewpointListForm;
import com.cloudbroker.bcs.platform.dav.bizapi.service.viewpoint.vo.ViewpointSpitslotUpvoteForm;
import com.cloudbroker.bcs.platform.dav.common.config.ConfigValues;
import com.cloudbroker.bcs.platform.dav.middle.context.HSBCPSBizDataContext;
import com.cloudbroker.bcs.platform.dav.middle.entity.Broker;
import com.cloudbroker.bcs.platform.dav.middle.entity.Customer;
import com.cloudbroker.bcs.platform.dav.middle.entity.PayWater;
import com.cloudbroker.bcs.platform.dav.middle.entity.Tenant;
import com.cloudbroker.bcs.platform.dav.middle.entity.ViewpointMsg;
import com.cloudbroker.bcs.platform.dav.middle.entity.ViewpointReply;
import com.cloudbroker.bcs.platform.dav.middle.entity.ViewpointTag;
import com.cloudbroker.bcs.platform.dav.middle.entityview.ViewpointNoLoginView;
import com.cloudbroker.bcs.platform.dav.middle.entityview.ViewpointReplyView;
import com.cloudbroker.bcs.platform.dav.middle.entityview.ViewpointView;
import com.cloudbroker.bcs.platform.dav.middle.extservice.ltb.LTBService;
import com.cloudbroker.bcs.platform.dav.middle.extservice.point.PointAddService;
import com.cloudbroker.bcs.platform.dav.middle.extservice.uc.UCenterService;
import com.cloudbroker.bcs.platform.dav.middle.service.BrokerManagerService;
import com.cloudbroker.bcs.platform.dav.middle.service.BrokerService;
import com.cloudbroker.bcs.platform.dav.middle.service.CustomerBrokerFollowService;
import com.cloudbroker.bcs.platform.dav.middle.service.PayWaterService;
import com.cloudbroker.bcs.platform.dav.middle.service.TenantService;
import com.cloudbroker.bcs.platform.dav.middle.service.UpvoteWaterService;
import com.cloudbroker.bcs.platform.dav.middle.service.ViewpointMsgService;
import com.cloudbroker.bcs.platform.dav.middle.service.ViewpointReplyService;
import com.cloudbroker.bcs.platform.dav.middle.service.ViewpointTagService;
import com.cloudbroker.bcs.platform.dav.middle.util.point.PointType;

@Service
public class ViewPointBizServiceImpl implements ViewPointBizService {

	@Resource
	private ViewpointReplyService viewpointReplyService;

	@Resource
	private ViewpointMsgService viewpointMsgService;

	@Resource
	private ViewpointTagService viewpointTagService;

	@Resource
	private UpvoteWaterService upvoteWaterService;

	@Resource
	private PayWaterService payWaterService;

	@Resource
	private BrokerManagerService brokerManagerService;

	@Resource
	private BrokerService brokerService;

	@Resource
	private CustomerBrokerFollowService customerBrokerFollowService;

	@Resource
	private TenantService tenantService;

	@Resource
	private LTBService ltbService;

	@Resource
	private UCenterService uCenterService;

	@Resource
	private PointAddService pointAddService;

	private void setContextAbstract(List<Long> payViewIds, ViewpointView view) {
		boolean flag = false;
		if (payViewIds != null && payViewIds.size() > 0) {
			flag = payViewIds.contains(view.getViewpoint_id());
		}
		if (flag) {
			view.setPay_flag(1);
		} else {
			view.setPay_flag(0);
			String abs = view.getViewpoint_abstract();
			int number = ConfigValues.getPayReadViewpointShow();
			if (abs != null && abs.length() > number) {
				abs = abs.substring(0, number) + "...";
			}
			view.setContext_abstract(abs);
		}
	}

	@Override
	public ViewpointFansList funcViewpointListByFans(ViewpointListForm param) {
		Customer customer = HSBCPSBizDataContext.getCustomerNoException();
		customer = customer == null ? new Customer() : customer;

		String viewpoint_tag_name = param.getViewpoint_tag_name();
		String position_str = param.getPosition_str();
		int request_num = ValueUtil.getInt(param.getRequest_num(), ConfigValues.getRequestNum());
		String status = param.getViewpoint_status();
		Long broker_id = HSBCPSBizDataContext.getBrokerId();
		String charge_flag = param.getCharge_flag();

		List<ViewpointView> msgs = this.viewpointMsgService.queryViewpointList(broker_id, status, viewpoint_tag_name,
				charge_flag, request_num, position_str);
		List<Long> msgIds = new ArrayList<>();
		if (msgs != null && msgs.size() > 0) {
			for (ViewpointView m : msgs) {
				msgIds.add(m.getViewpoint_id());
			}
			if (customer.getCustomer_id() != null) {
				List<Long> ups = this.upvoteWaterService.queryByCustomerId(customer.getCustomer_id(), broker_id, msgIds,
						String.valueOf(UpvoteStatus.VIEWPOINT_UPVOTE.getVal()));
				List<Long> payViewIds = this.payWaterService.queryPayViewIds(customer.getCustomer_id(), msgIds,
						PaySource.VIEWPOINT_READ);
				for (ViewpointView view : msgs) {
					view.setUpvote_flag(0);
					if (ups != null && ups.contains(view.getViewpoint_id().longValue())) {
						view.setUpvote_flag(1);
					}
					if (view.getPrice() != null && BigDecimal.ZERO.compareTo(view.getPrice()) == -1) {
						view.setPay_num(view.getReward_num());
						setContextAbstract(payViewIds, view);
					}
				}
			}

		}
		ViewpointFansList ret = new ViewpointFansList();
		ret.newDataList().addAll(msgs);

		return VOUtil.setSuccessResult(ret);
	}

	@Override
	public ViewpointCommentResult funcCommentViewNologin(ViewpointCommentForm param) {
		return getCommView(param);
	}

	private ViewpointCommentResult getCommView(ViewpointCommentForm param) {
		String viewpoint_id = param.getViewpoint_id();
		String position_str = param.getPosition_str();
		Integer request_num = ValueUtil.getInt(param.getRequest_num(), ConfigValues.getRequestNum());

		ViewpointMsg msg = this.viewpointMsgService.getSendViewpointByID(viewpoint_id);
		if (null == msg) {
			return VOUtil.genErrorResult(SysMgtErr.NO_SUCH_VIEWPOINT_ID, ViewpointCommentResult.class);
		}
		List<ViewpointReplyView> msgs = this.viewpointReplyService.queryViewpointReplyViews(viewpoint_id, request_num,
				position_str);
		// FIXME 为了使大V头像显示最新的，暂时这么搞
		Broker broker = brokerService.getById(msg.getBroker_id(), msg.getTenant_id());
		if (msgs != null) {
			for (ViewpointReplyView view : msgs) {
				if (view.getBroker_flag() != null && view.getBroker_flag() == 1) {
					view.setCustomer_avatar(broker.getBroker_avatar());
					view.setCustomer_name(broker.getBroker_name());
				}
				if (view.getQuotes() != null) {
					for (ViewpointReplyData quote : view.getQuotes()) {
						if (quote.getBroker_flag() != null && quote.getBroker_flag() == 1) {
							quote.setCustomer_avatar(broker.getBroker_avatar());
							quote.setCustomer_name(broker.getBroker_name());
						}
					}
				}
			}
		}
		ViewpointCommentResult result = new ViewpointCommentResult();
		String viewpoint_tag_id = msg.getViewpoint_tag().toString();
		ViewpointTag viewpointTag = this.viewpointTagService.get(viewpoint_tag_id);
		if (viewpointTag != null) {
			result.setTag_name(viewpointTag.getTag_name());
		}
		result.setBroker_id(msg.getBroker_id());
		result.setUpvote_num(msg.getUpvote_num());
		result.setComment_num(msg.getComment_num());
		result.setOss_prefix(ConfigValues.getInterfaceOssAddr());
		result.setReward_num(msg.getReward_num());
		result.setViewpoint_tag_id(msg.getViewpoint_tag());
		result.setViewpoint_content(msg.getViewpoint_content());
		result.setSend_time(DateTimeUtil.toString(msg.getSend_time(), DateTimeUtil.FORMAT_YYYYMMDDHHMMSS));
		result.setUpdate_time(DateTimeUtil.toString(msg.getUpdate_time(), DateTimeUtil.FORMAT_YYYYMMDDHHMMSS));
		result.newDataList().addAll(msgs);
		return VOUtil.setSuccessResult(result);
	}

	@Override
	public ViewpointCommentResult funcCommentView(ViewpointCommentForm param) {
		ViewpointCommentResult result = getCommView(param);
		Customer customer = HSBCPSBizDataContext.getCustomer();
		Long broker_id = HSBCPSBizDataContext.getBrokerId();
		List<Long> replyIds = new ArrayList<>();
		List<ViewpointReplyData> replys = result.getData_list();
		// 判断是否点赞
		List<Long> msgIds = new ArrayList<>();
		msgIds.add(Long.parseLong(param.getViewpoint_id()));
		List<Long> temp = upvoteWaterService.queryByCustomerId(customer.getCustomer_id(), broker_id, msgIds,
				String.valueOf(UpvoteStatus.VIEWPOINT_UPVOTE.getVal()));
		if (temp == null || temp.isEmpty()) {
			result.setUpvote_flag(0);
		} else {
			result.setUpvote_flag(1);
		}
		if (replys != null && replys.size() > 0) {
			for (ViewpointReplyData m : replys) {
				replyIds.add(m.getReply_id());
			}
			List<Long> ups = this.upvoteWaterService.queryByCustomerId(customer.getCustomer_id(), broker_id, replyIds,
					String.valueOf(UpvoteStatus.COMMENT_UPVOTE.getVal()));
			List<Long> splits = this.upvoteWaterService.queryByCustomerId(customer.getCustomer_id(), broker_id,
					replyIds, String.valueOf(UpvoteStatus.COMMENT_SPITSLOT.getVal()));
			for (ViewpointReplyData view : replys) {
				view.setUpvote_flag(0);
				if (ups != null && ups.contains(view.getReply_id().longValue())) {
					view.setUpvote_flag(1);
				}
				view.setSpitslot_flag(0);
				if (splits != null && splits.contains(view.getReply_id().longValue())) {
					view.setSpitslot_flag(1);
				}
			}
		}
		return VOUtil.setSuccessResult(result);
	}

	@Override
	public ViewpointCommentResult funcCommentViewByBroker(ViewpointCommentForm param) {
		ViewpointCommentResult result = getCommView(param);
		return VOUtil.setSuccessResult(result);
	}

	@Override
	public CommentSaveResult funcCommentSave(ViewpointCommentSaveForm param) {

		String viewpoint_id = param.getViewpoint_id();
		ViewpointMsg msg = this.viewpointMsgService.getSendViewpointByID(viewpoint_id);
		if (null == msg) {
			return VOUtil.genErrorResult(SysMgtErr.NO_SUCH_VIEWPOINT_ID, CommentSaveResult.class);
		}

		String comment_content = param.getComment_content();
		String reply_from = param.getReply_from();
		String reply_type = param.getReply_type();

		ViewpointReply reply = new ViewpointReply();
		// 如果是粉丝评论则需要检验
		if (reply_type.equals(String.valueOf(ReplyType.FANS.getVal()))) {
			Customer customer = HSBCPSBizDataContext.getCustomer();
			if (msg.getPrice() != null && msg.getPrice().compareTo(BigDecimal.ZERO) == 1) {
				PayWater pw = payWaterService.getViewpointPayWater(msg.getViewpoint_id(),
						PaySource.VIEWPOINT_READ.getVal(), customer.getCustomer_id());
				if (pw == null) {
					return VOUtil.genErrorResult(SysMgtErr.NO_PAY, CommentSaveResult.class);
				}
			}
			reply.setCustomer_id(customer.getCustomer_id());
			reply.setCustomer_name(customer.getCustomer_name());
			reply.setAvatar(customer.getAvatar());
			reply.setBroker_flag(0);
			StringBuffer buffer = new StringBuffer("[{\"type\":\"text\",\"body\":\"");
			reply.setReply_content(buffer.append(comment_content).append("\"}]").toString());
		}

		if (reply_type.equals(String.valueOf(ReplyType.DAV.getVal()))) {
			// 投顾评论保存投顾信息
			Broker b = HSBCPSBizDataContext.getBroker();
			reply.setCustomer_id(b.getBroker_id());
			reply.setCustomer_name(b.getBroker_name());
			reply.setAvatar(b.getBroker_avatar());
			reply.setBroker_flag(1);
			reply.setReply_content(comment_content);
		}

		reply.setViewpoint_id(Long.parseLong(viewpoint_id));
		if (null != reply_from && !"".equals(reply_from)) {
			reply.setReply_from(Long.parseLong(reply_from));
		}
		reply.setUpvote_num(0);
		reply.setSpitslot_num(0);
		reply.setReply_time(new Date());
		reply.setPosition_str(PositionStrUtil.getPositionStr());
		reply.setDelete_flag(0);
		this.viewpointReplyService.save(reply);

		// 增加积分
		Customer c = HSBCPSBizDataContext.getCustomerNoException();
		if (c != null) {
			pointAddService.addPoint(c.getMobile(), PointType.COMMENT, c.getUser_id(), c.getTenant_id());
		}

		Integer comment_num = msg.getComment_num();
		comment_num = comment_num == null ? 1 : comment_num + 1;
		msg.setComment_num(comment_num);
		msg.setUpdate_time(new Date());
		this.viewpointMsgService.update(msg);
		CommentSaveResult ret = new CommentSaveResult();
		ret.setComment_num(comment_num);
		return VOUtil.setSuccessResult(ret);
	}

	@Override
	public ViewpointSpitslotUpvoteResult funcCommentSpitslotUpvote(ViewpointSpitslotUpvoteForm param) {

		String replay_id = param.getReplay_id();
		String spitslot_upvote_type = param.getSpitslot_upvote_type();
		Long broker_id = HSBCPSBizDataContext.getBrokerId();
		Customer customer = HSBCPSBizDataContext.getCustomer();
		Long customer_id = customer.getCustomer_id();
		String target_type = "";

		ViewpointReply msg = this.viewpointReplyService.get(replay_id);
		if (null == msg) {
			return VOUtil.genErrorResult(SysMgtErr.NO_SUCH_VIEWPOINT_COMM_ID, ViewpointSpitslotUpvoteResult.class);
		}

		if ("1".equals(spitslot_upvote_type)) {
			Integer spitslot_num = ValueUtil.getIntObj(msg.getSpitslot_num(), 0);
			target_type = String.valueOf(UpvoteStatus.COMMENT_SPITSLOT.getVal());
			int count = this.upvoteWaterService.upvoteCount(customer_id, broker_id, replay_id, target_type);
			if (count > 0) {
				return VOUtil.genErrorResult(QuestionErr.HAS_SPITSLOT, ViewpointSpitslotUpvoteResult.class);
			}
			msg.setSpitslot_num(++spitslot_num);
		} else if ("2".equals(spitslot_upvote_type)) {
			Integer upvote_num = ValueUtil.getIntObj(msg.getUpvote_num(), 0);
			target_type = String.valueOf(UpvoteStatus.COMMENT_UPVOTE.getVal());
			int count = this.upvoteWaterService.upvoteCount(customer_id, broker_id, replay_id, target_type);
			if (count > 0) {
				return VOUtil.genErrorResult(QuestionErr.HAS_UPVOTE, ViewpointSpitslotUpvoteResult.class);
			}
			msg.setUpvote_num(++upvote_num);
		}

		viewpointReplyService.updateCAS(msg);
		this.upvoteWaterService.saveUpvoteWater(customer_id, broker_id, replay_id, target_type);
		// 增加积分
		pointAddService.addPoint(customer.getMobile(), PointType.UPVOTE, customer.getUser_id(),
				customer.getTenant_id());
		ViewpointSpitslotUpvoteResult result = new ViewpointSpitslotUpvoteResult();
		result.setSpitslot_num(msg.getSpitslot_num());
		result.setUpvote_num(msg.getUpvote_num());
		return VOUtil.setSuccessResult(result);
	}

	@Override
	public ReplyListData funcViewpointReplayDetailList(ReplyListForm param) {
		List<ViewpointReplyView> list = viewpointReplyService.queryReplyFroms(Integer.valueOf(param.getReply_id()),
				param.getPosition_str(), ValueUtil.getInt(param.getRequest_num(), ConfigValues.getRequestNum()));
		ReplyListData ret = new ReplyListData();
		ret.newDataList().addAll(list);
		return VOUtil.setSuccessResult(ret);
	}

	@Override
	public ViewpointContentData funcViewpointContent(ViewPointIdForm param) {
		ViewpointMsg vp = viewpointMsgService.getSendViewpointByID(param.getViewpoint_id());
		if (vp == null) {
			return VOUtil.genErrorResult(SysMgtErr.NO_SUCH_VIEWPOINT_ID, ViewpointContentData.class);
		}
		if (vp.getPrice() != null && vp.getPrice().compareTo(BigDecimal.ZERO) == 1) {
			PayWater example = new PayWater();
			example.setCustomer_id(HSBCPSBizDataContext.getCustomer().getCustomer_id());
			example.setTarget_id(Long.valueOf(param.getViewpoint_id()));
			List<PayWater> water = payWaterService.query(example);
			if (water == null || water.isEmpty()) {
				return VOUtil.genErrorResult(SysMgtErr.NO_PAY, ViewpointContentData.class);
			}
		}
		ViewpointContentData map = new ViewpointContentData();
		map.setViewpoint_content(vp.getViewpoint_content());
		map.setViewpoint_abstract(vp.getViewpoint_abstract());
		map.setViewpoint_img_audio(vp.getViewpoint_img_audio());
		return VOUtil.setSuccessResult(map);
	}

	@Override
	public HSBCPSBackVO funcViewpointContentConvert(HSBCPSVO param) {
		List<ViewpointMsg> list = null;
		String positionStr = "0";
		Integer requestNum = 30;
		do {
			list = viewpointMsgService.queryByPositionStr(positionStr, requestNum);
			if (list == null || list.isEmpty()) {
				break;
			}
			convertContent(list);
			positionStr = list.get(list.size() - 1).getPosition_str();
		} while (true);
		return VOUtil.genSuccessBackVo();
	}

	private void convertContent(List<ViewpointMsg> list) {
		for (ViewpointMsg viewpointMsg : list) {
			if (viewpointMsg.getViewpoint_content().startsWith("[")) {
				try {
					JSONArray jsonArray = JSONUtil.toJsonArray(viewpointMsg.getViewpoint_content());
					String abs = "";
					List<Object> removeObj = new ArrayList<>();
					for (Object object : jsonArray) {
						if (object instanceof Map) {
							@SuppressWarnings("unchecked")
							Map<String, Object> map = (Map<String, Object>) object;
							if (map.get("body") == null || "[]".equals(map.get("body").toString())) {
								removeObj.add(map);
								continue;
							}
							if ("text".equals(map.get("type"))) {
								abs = (String) map.get("body");
								removeObj.add(map);
							}
							if ("stock".equals(map.get("type"))) {
								map.put("type", "vstock");
							}
						}
					}
					if (!removeObj.isEmpty()) {
						jsonArray.removeAll(removeObj);
					}
					viewpointMsg.setViewpoint_img_audio(jsonArray.toJSONString());
					viewpointMsg.setViewpoint_abstract(abs);
					viewpointMsg.setViewpoint_content(
							"{\"title\":\"\",\"author\":\"\",\"content\":\"<p>" + abs + "</p>\"}");
					viewpointMsgService.update(viewpointMsg);
				} catch (Exception e) {
				}
			}
		}
	}

	@Override
	public ViewpointNoLoginList funcViewpointListByTenant(ListQueryVO param) {
		Long customer_id = HSBCPSBizDataContext.getClientId();
		Integer request_num = ValueUtil.getInt(param.getRequest_num(), ConfigValues.getRequestNum());
		String position_str = param.getPosition_str();
		// 查询所有投顾观点列表
		List<ViewpointNoLoginView> list = this.viewpointMsgService.qryNoLoginList(position_str, request_num);
		// 查询粉丝关注的投顾列表
		List<Long> brokers = this.customerBrokerFollowService.getBrokersByCustomerId(customer_id);
		if (list != null && list.size() > 0) {
			for (int i = 0, j = list.size(); i < j; i++) {
				ViewpointNoLoginView view = list.get(i);
				if (null != brokers && brokers.size() > 0) {
					if (brokers.contains(view.getBroker_id())) {
						view.setFollow_type("1");
					} else {
						view.setFollow_type("0");
					}
				} else {
					view.setFollow_type("0");
				}
			}
		}
		ViewpointNoLoginList result = new ViewpointNoLoginList();
		result.newDataList().addAll(list);
		return VOUtil.setSuccessResult(result);
	}

	@Override
	public ViewpointNoLoginList funcViewpointListByTenantNologin(TenantKeyListQryForm param) {
		Integer request_num = ValueUtil.getInt(param.getRequest_num(), ConfigValues.getRequestNum());
		String position_str = param.getPosition_str();
		Tenant tenant = tenantService.getByKey(param.getTenant_key());
		if (tenant == null) {
			return VOUtil.genErrorResult(ClientErr.TENANT_NOT_EXIST, ViewpointNoLoginList.class);
		}
		HSBCPSBizDataContext.setTenantId(tenant.getTenant_id());
		// 查询所有投顾观点列表
		List<ViewpointNoLoginView> list = this.viewpointMsgService.qryNoLoginList(position_str, request_num);
		if (list != null && list.size() > 0) {
			for (int i = 0, j = list.size(); i < j; i++) {
				ViewpointNoLoginView view = list.get(i);
				view.setFollow_type("0");
			}
		}
		ViewpointNoLoginList result = new ViewpointNoLoginList();
		result.newDataList().addAll(list);
		return VOUtil.setSuccessResult(result);
	}

	@Override
	public SelfViewpointResult funcSelfViewpointsList(SelfViewpointForm form) {
		SelfViewpointResult result = new SelfViewpointResult();
		String key_word = form.getKey_word();
		Integer page_no = ValueUtil.getInt(form.getPage_no(), 1);
		Integer page_count = ValueUtil.getInt(form.getPage_count(), ConfigValues.getRequestNum());
		String field_sort = "send_time";
		String sort_order = "desc";
		String viewpoint_review_type = "0";
		List<Map<String, Object>> list = SearchUtil.getInstance().getSelfMediaList(key_word, page_no, page_count,
				field_sort, sort_order, viewpoint_review_type);
		List<SelfViewpointList> selfViewpointLists = new ArrayList<>();
		// FIXME: 2016/12/28 确保这里branch_no是只有一个的，而不是逗号隔开
		String public_review_flag = tenantService.getSelfFlagByBranchNo(form.getBranch_no());
		if (StringUtil.isEmpty(public_review_flag) || public_review_flag == "nil") {
			public_review_flag = "0";
		}
		if (list != null && list.size() > 0) {
			for (Map<String, Object> map : list) {
				SelfViewpointList selfViewpointList = new SelfViewpointList();
				selfViewpointList.setBroker_name(ValueUtil.getString(map.get("broker_name")));
				selfViewpointList.setViewpoint_abstract(ValueUtil.getString(map.get("label")));
				selfViewpointList.setSelf_viewpoint_id(ValueUtil.getLong(map.get("id")));
				String content = ValueUtil.getString(map.get("content"));
				selfViewpointList.setContent(ViewpointMsgFormatUtil.parseContext(content));
				String time = ValueUtil.getString(map.get("send_time"));
				if (!ValueUtil.isEmpty(time)) {
					StringBuilder stringBuilder = new StringBuilder();
					stringBuilder.append(time.substring(0, 4)).append("-").append(time.substring(4, 6)).append("-")
							.append(time.substring(6, 8)).append(" ").append(time.substring(8, 10)).append(":")
							.append(time.substring(10, 12)).append(":").append(time.substring(12, 14));
					time = stringBuilder.toString();
				}
				selfViewpointList.setSend_time(time);
				selfViewpointList.setTitle(ValueUtil.getString(map.get("viewpoint_title")));
				String public_review_status = ValueUtil.getString(map.get("public_review_status"));
				selfViewpointList.setPublic_review_status(public_review_status);
				Map<String, Object> totalmap = list.get(0);
				result.setTotal_item_num(ValueUtil.getInt(totalmap.get("total")));
				selfViewpointLists.add(selfViewpointList);
			}
			result.setData_list(selfViewpointLists);
			result.setPublic_review_flag(public_review_flag);
			result.setPage_no(page_no);
			return VOUtil.setSuccessResult(result);
		} else {
			return VOUtil.genErrorResult(HSBCPSErrorCodes.SysMgtErr.NO_SUCH_HISTORY_RECORD, SelfViewpointResult.class);
		}
	}

	@Override
	public HSBCPSBackVO funcSelfViewpointReview(SelfViewpointReviewForm form) {
		String public_review_flag = form.getPublic_review_flag();
		List<Long> tenantIds = tenantService.getTenantIdsByBranchNo(form.getBranch_no());
		if (tenantIds == null || tenantIds.isEmpty()) {
			return VOUtil.genErrorResultVo(HSBCPSErrorCodes.OperateErr.TENANT_NOT_EXIST_BY_BRANCH);
		}
		tenantService.updateSelfFlagByTenantIds(tenantIds, public_review_flag);
		HSBCPSBackVO result = new HSBCPSBackVO();
		return VOUtil.setSuccessResult(result);
	}

	@Override
	public HSBCPSBackVO funcSelfViewpointReviewStatus(SelfViewpointStatusForm form) {
		SearchUtil.getInstance().updateSearchById(form.getSelf_viewpoint_id(), form.getPublic_review_status());
		viewpointMsgService.funcViewpointReviewByIdManage(form.getSelf_viewpoint_id(), form.getPublic_review_status());
		HSBCPSBackVO result = new HSBCPSBackVO();
		return VOUtil.setSuccessResult(result);
	}

	@Override
	public SelfViewpointBack funcSelfViewpointById(SelfViewpointByIdForm form) {
		String selfViewpoinId = form.getSelf_viewpoint_id();
		Map<String, Object> map = SearchUtil.getInstance().getSelfViewpointById(selfViewpoinId);
		if (ValueUtil.isEmpty(map)) {
			return VOUtil.genErrorResult(HSBCPSErrorCodes.SysMgtErr.NO_SUCH_HISTORY_RECORD, SelfViewpointBack.class);
		}
		SelfViewpointBack result = new SelfViewpointBack();
		result.setBroker_name(ValueUtil.getString(map.get("broker_name")));
		result.setViewpoint_abstract(ValueUtil.getString(map.get("label")));
		result.setSelf_viewpoint_id(ValueUtil.getLong(map.get("id")));
		String content = ValueUtil.getString(map.get("content"));
		result.setContent(ValueUtil.getString(ViewpointMsgFormatUtil.parseContext(content)));
		String time = ValueUtil.getString(map.get("send_time"));
		if (!ValueUtil.isEmpty(time)) {
			StringBuilder stringBuilder = new StringBuilder();
			stringBuilder.append(time.substring(0, 4)).append("-").append(time.substring(4, 6)).append("-")
					.append(time.substring(6, 8)).append(" ").append(time.substring(8, 10)).append(":")
					.append(time.substring(10, 12)).append(":").append(time.substring(12, 14));
			time = stringBuilder.toString();
		}
		result.setSend_time(time);
		result.setTitle(ValueUtil.getString(map.get("viewpoint_title")));
		return VOUtil.setSuccessResult(result);
	}

	@Override
	public DynamicListResult funcGetDynamicList(DynamicListForm form) {
		Integer request_num = ValueUtil.getInt(form.getRequest_num(), ConfigValues.getRequestNum());
		String position_str = form.getPosition_str();
		String company_id = form.getCompany_id();
		DynamicListResult result = new DynamicListResult();
		if (HSBCPSBizDataContext.getCustomerNoException() != null) {
			result.setUser_token(form.getUser_token());
		}
		String LTB_user_token = getLTBToken(form.getUser_token(), company_id);
		result.setLTB_user_token(LTB_user_token);
		List<DynamicListData> dynamicListDatas;
		List<LTBDynamicList> LTBDynamicLists;
		if (ValueUtil.getString(DynamicType.VIEWPOINT.getVal()).equals(form.getDynamic_type())) {
			// 查询所有投顾观点列表
			List<ViewpointNoLoginView> viewpointNoLoginViews = this.viewpointMsgService.qryNoLoginList(position_str,
					request_num);
			if(!ValueUtil.isEmpty(form.getUser_token())){
				setUpvoteFlag(viewpointNoLoginViews);
			}
			dynamicListDatas = getDynamicListDataByViewpoint(viewpointNoLoginViews);
			result.setData_list(dynamicListDatas);
			return VOUtil.setSuccessResult(result);
		} else if (ValueUtil.getString(DynamicType.DYNAMIC.getVal()).equals(form.getDynamic_type())) {
			LTBDynamicLists = getDynamicListByT2(request_num, LTB_user_token, position_str, company_id);
			if (LTBDynamicLists == null) {
				return VOUtil.genErrorResult(SysMgtErr.LTB_DYNAMIC_LIST_NULL, DynamicListResult.class);
			}
			dynamicListDatas = getLTBDynamicListByCombine(LTBDynamicLists);
			dynamicListDatas = getRealtimeQryByT2(dynamicListDatas, company_id); // 组合结果添加一个realtime_qry字段，json字符串，broker_avatar头像数据
			if (dynamicListDatas == null) {
				return VOUtil.genErrorResult(SysMgtErr.LTB_DYNAMIC_LIST_NULL, DynamicListResult.class);
			}
			result.setData_list(dynamicListDatas);
			return VOUtil.setSuccessResult(result);
		} else {
			String viewpoint_position_str = position_str.substring(0, position_str.indexOf("-"));
			String dynamic_position_str = position_str.substring(position_str.indexOf("-") + 1, position_str.length());
			List<ViewpointNoLoginView> viewpointNoLoginViews = this.viewpointMsgService
					.qryNoLoginList(viewpoint_position_str, request_num);
			if(!ValueUtil.isEmpty(form.getUser_token())){
				setUpvoteFlag(viewpointNoLoginViews);
			}
			LTBDynamicLists = getDynamicListByT2(request_num, LTB_user_token, dynamic_position_str, company_id);
			dynamicListDatas = getDynamicListDataByViewpoint(viewpointNoLoginViews); // 观点
			List<DynamicListData> listByCombine = getLTBDynamicListByCombine(LTBDynamicLists); // 组合
			dynamicListDatas.addAll(listByCombine); // 观点加组合
			Collections.sort(dynamicListDatas, new Comparator<DynamicListData>() {
				@Override
				public int compare(DynamicListData o1, DynamicListData o2) {
					return o2.getSend_time().compareTo(o1.getSend_time());
				}
			});
			dynamicListDatas = getRealtimeQryByT2(dynamicListDatas, company_id); // 组合结果添加realtime_qry字段，json字符串
																					// ,broker_avatar头像数据，签约状态字段；
			if (LTBDynamicLists == null) {
				return VOUtil.genErrorResult(SysMgtErr.LTB_DYNAMIC_LIST_NULL, DynamicListResult.class);
			}
			List<DynamicListData> dynamicListDatasResult = new ArrayList<>();
			Integer num = request_num > dynamicListDatas.size() ? dynamicListDatas.size() : request_num;
			for (int i = 0; i < num; i++) {
				StringBuilder position = new StringBuilder();
				if (ValueUtil.getString(DynamicType.VIEWPOINT.getVal())
						.equals(dynamicListDatas.get(i).getDynamic_type())) {
					viewpoint_position_str = dynamicListDatas.get(i).getPosition_str();
					position.append(viewpoint_position_str).append("-").append(dynamic_position_str);
				} else if (ValueUtil.getString(DynamicType.DYNAMIC.getVal())
						.equals(dynamicListDatas.get(i).getDynamic_type())) {
					dynamic_position_str = dynamicListDatas.get(i).getPosition_str();
					position.append(viewpoint_position_str).append("-").append(dynamic_position_str);
				}
				dynamicListDatas.get(i).setPosition_str(position.toString());
				dynamicListDatasResult.add(dynamicListDatas.get(i));
			}
			result.setData_list(dynamicListDatasResult);
			return VOUtil.setSuccessResult(result);
		}
	}

	@Override
	public LTBRealtimeQryResult funcRealtimeQryLoadMore(LTBRealtimeQryListForm form) {
		Integer request_num = ValueUtil.getInt(form.getRequest_num(), ConfigValues.getRequestNum());
		// String position_str = form.getPosition_str();
		String position_str = "0".equals(form.getPosition_str()) ? "" : form.getPosition_str();
		Map<String, Object> param = new HashMap<>();
		param.put("user_token", form.getLtb_user_token());
		param.put("combi_code", form.getCombi_code());
		param.put("sendercomp_id", form.getSendercomp_id());
		String url = ConfigUtil.get("ltb.realtime.qry.url", "http://10.139.55.105/trans/") + "combine_basic_info_qry";
		String LTB_combine_basic_info = HttpsUtil.doHttpsPost(url, param);
		JSONObject ltb_obj1 = JSON.parseObject(LTB_combine_basic_info);
		String ltb_str1 = ltb_obj1.getString("data");
		JSONArray ltb_arr1 = JSON.parseArray(ltb_str1);
		String ltb_str2 = ltb_arr1.getString(0);
		JSONObject ltb_obj2 = JSON.parseObject(ltb_str2);
		String data_list = ltb_obj2.getString("data_list");
		JSONArray ltb_jsonArray = JSONUtil.toJsonArray(data_list);
		String ltb_busin_account = ltb_jsonArray.getJSONObject(0).getString("busin_account");
		String ltb_user_id = ltb_jsonArray.getJSONObject(0).getString("user_id");
		String businsys_no = ltb_jsonArray.getJSONObject(0).getString("businsys_no");
		String busin_account = ltb_busin_account;
		Map<String, Object> params = new HashMap<>();
		params.put("user_token", form.getLtb_user_token());
		params.put("user_id", ltb_user_id);
		params.put("sendercomp_id", form.getSendercomp_id());
		params.put("busin_account", busin_account);
		params.put("op_entrust_way", LTBInfo.op_entrust_way);
		params.put("op_station", LTBInfo.op_station);
		params.put("businsys_no", businsys_no);
		params.put("company_id", "91000");
		params.put("position_str", position_str);
		params.put("request_num", request_num);
		url = ConfigUtil.get("ltb.realtime.qry.url", "http://10.139.55.105/trans/") + "comentrust_detail_qry";
		String LTB_realtime_qry = HttpsUtil.doHttpsPost(url, params);
		if (LTB_realtime_qry == null) {
			return VOUtil.genErrorResult(SysMgtErr.LTB_REALTIME_QRY_ERR, LTBRealtimeQryResult.class);
		}
		JSONObject obj1 = JSON.parseObject(LTB_realtime_qry);
		String str1 = obj1.getString("data");
		JSONArray arr1 = JSON.parseArray(str1);
		String str2 = arr1.getString(0);
		JSONObject obj2 = JSON.parseObject(str2);
		data_list = obj2.getString("data_list");
		JSONArray jsonArray = JSONUtil.toJsonArray(data_list);
		List<LTBRealtimeQryData> ltbRealtimeQryDatas = new ArrayList<>();
		for (int i = 0; i < jsonArray.size(); i++) {
			LTBRealtimeQryData ltbRealtimeQryData = new LTBRealtimeQryData();
			com.alibaba.fastjson.JSONObject jsonObject = jsonArray.getJSONObject(i);
			ltbRealtimeQryData.setBusiness_amount(jsonObject.getString("business_amount"));
			ltbRealtimeQryData.setBusin_account(jsonObject.getString("busin_account"));
			ltbRealtimeQryData.setExchange_type(jsonObject.getString("aasexch_type"));
			ltbRealtimeQryData.setBusiness_price(jsonObject.getString("business_price"));
			ltbRealtimeQryData.setBusiness_time(ValueUtil.getInt(jsonObject.getString("business_time")));
			ltbRealtimeQryData.setEntrust_bs(jsonObject.getString("entrust_bs"));
			ltbRealtimeQryData.setInit_date(ValueUtil.getInt(jsonObject.getString("init_date")));
			ltbRealtimeQryData.setProd_code(jsonObject.getString("prod_code"));
			ltbRealtimeQryData.setSdc_account(jsonObject.getString("sdc_account"));
			ltbRealtimeQryData.setSerial_no(ValueUtil.getLong(jsonObject.getString("serial_no")));
			ltbRealtimeQryData.setProd_name(jsonObject.getString("prod_name"));
			ltbRealtimeQryData.setPosition_str(jsonObject.getString("position_str"));
			ltbRealtimeQryDatas.add(ltbRealtimeQryData);
		}
		LTBRealtimeQryResult result = new LTBRealtimeQryResult();
		result.setData_list(ltbRealtimeQryDatas);
		return VOUtil.setSuccessResult(result);
	}

	public String formatDate(String time) {
		if (!ValueUtil.isEmpty(time)) {
			StringBuilder stringBuilder = new StringBuilder();
			stringBuilder.append(time.substring(0, 4)).append("-").append(time.substring(4, 6)).append("-")
					.append(time.substring(6, 8)).append(" ").append(time.substring(8, 10)).append(":")
					.append(time.substring(10, 12)).append(":").append(time.substring(12, 14));
			time = stringBuilder.toString();
		}
		return time;
	}

	private void setUpvoteFlag(List<ViewpointNoLoginView> viewpointNoLoginViews){
		Long broker_id = HSBCPSBizDataContext.getBrokerId();
		// 判断是否点赞
		List<Long> msgIds = new ArrayList<>();
		for(ViewpointNoLoginView view : viewpointNoLoginViews){
			msgIds.add(view.getViewpoint_id());
		}
		List<Long> temp = upvoteWaterService.queryByCustomerId(HSBCPSBizDataContext.getCustomer().getCustomer_id(), broker_id, msgIds,
				String.valueOf(UpvoteStatus.VIEWPOINT_UPVOTE.getVal()));
		if(ValueUtil.isEmpty(temp)){
			for(ViewpointNoLoginView view : viewpointNoLoginViews){
				view.setUpvote_flag("0");
			}
		}else {
			for(int i = 0 ; i < viewpointNoLoginViews.size(); i++){
				if(temp.contains(viewpointNoLoginViews.get(i).getViewpoint_id())){
					viewpointNoLoginViews.get(i).setUpvote_flag("1");
				}else{
					viewpointNoLoginViews.get(i).setUpvote_flag("0");
				}
			}
		}
	}

	public List<DynamicListData> getDynamicListDataByViewpoint(List<ViewpointNoLoginView> viewpointNoLoginViews) {
		List<DynamicListData> lists = new ArrayList<>();
		for (ViewpointNoLoginView view : viewpointNoLoginViews) {
			DynamicListData dynamicListData = new DynamicListData();
			dynamicListData.setViewpoint_id(view.getViewpoint_id());
			dynamicListData.setBroker_name(view.getBroker_name());
			dynamicListData.setSend_time(view.getSend_time());
			dynamicListData.setBroker_avatar(view.getBroker_avatar());
			dynamicListData.setBroker_id(view.getBroker_id());
			dynamicListData.setBroker_key(view.getBroker_key());
			dynamicListData.setComment_num(view.getComment_num());
			dynamicListData.setContext_abstract(view.getContext_abstract());
			dynamicListData.setFollow_type(view.getFollow_type());
			dynamicListData.setReward_num(view.getReward_num());
			dynamicListData.setUpdate_time(view.getUpdate_time());
			dynamicListData.setUpvote_flag(view.getUpvote_flag());
			dynamicListData.setUpvote_num(view.getUpvote_num());
			dynamicListData.setViewpoint_abstract(view.getViewpoint_abstract());
			dynamicListData.setViewpoint_img_audio(view.getViewpoint_img_audio());
			dynamicListData.setViewpoint_status(view.getViewpoint_status());
			dynamicListData.setViewpoint_tag_name(view.getViewpoint_tag_name());
			dynamicListData.setDynamic_type(ValueUtil.getString(DynamicType.VIEWPOINT.getVal()));
			dynamicListData.setPosition_str(view.getPosition_str());
			lists.add(dynamicListData);
		}
		return lists;
	}

	public List<DynamicListData> getLTBDynamicListByCombine(List<LTBDynamicList> LTBDynamicLists) {
		List<DynamicListData> lists = new ArrayList<>();
		for (LTBDynamicList ltbDynamicList : LTBDynamicLists) {
			DynamicListData dynamicListData = new DynamicListData();
			dynamicListData.setSendercomp_id(ltbDynamicList.getSendercomp_id());
			dynamicListData.setCombi_code(ltbDynamicList.getCombi_code());
			dynamicListData.setCombi_name(ltbDynamicList.getCombi_name());
			dynamicListData.setBroker_id(ValueUtil.getLong(ltbDynamicList.getAdviser_id()));
			dynamicListData.setClient_name(ltbDynamicList.getClient_name());
			String business_time = ltbDynamicList.getBusiness_time().length() < 6
					? "0" + ltbDynamicList.getBusiness_time() : ltbDynamicList.getBusiness_time();
			String time = ValueUtil.getString(ltbDynamicList.getInit_date()) + business_time;
			dynamicListData.setSend_time(formatDate(time));
			dynamicListData.setInit_date(ValueUtil.getInt(ltbDynamicList.getInit_date()));
			dynamicListData.setBusiness_time(ValueUtil.getInt(ltbDynamicList.getBusiness_time()));
			dynamicListData.setPosition_str(ltbDynamicList.getPosition_str());
			dynamicListData.setDynamic_type(ValueUtil.getString(DynamicType.DYNAMIC.getVal()));
			dynamicListData.setRealtime_qry(ltbDynamicList.getRealtime_qry());
			dynamicListData.setLtb_user_token(ltbDynamicList.getUser_token());
			lists.add(dynamicListData);
		}
		return lists;
	}

	public List<LTBDynamicList> getDynamicListByT2(Integer request_num, String LTB_user_token, String position_str,
			String company_id) {
		List<LTBDynamicList> lists = new ArrayList<>();
		Map<String, Object> params = new HashMap<>();
		params.put("user_token", LTB_user_token);
		params.put("company_id", company_id);
		params.put("sendercomp_id", company_id);
		params.put("op_branch_no", ValueUtil.getString(LTBInfo.op_branch_no));
		params.put("branch_no", LTBInfo.branch_no);
		params.put("op_entrust_way", LTBInfo.op_entrust_way);
		params.put("op_station", LTBInfo.op_station);
		params.put("request_num", request_num);
		String url = ConfigUtil.get("ltb.realtime.qry.url", "http://10.139.55.105/trans/") + "realtime_combine_qry";
		String LTB_realtime_qry = HttpsUtil.doHttpsPost(url, params);
		if (LTB_realtime_qry == null || getLtbResultErrorInfo(LTB_realtime_qry)) {
			return null;
		}
		JSONArray jsonArray = getJsonArrayFormStr(LTB_realtime_qry);
		for (int i = 0; i < jsonArray.size(); i++) {
			int num = 1;
			LTBDynamicList ltbDynamicList = new LTBDynamicList();
			com.alibaba.fastjson.JSONObject jsonObject = jsonArray.getJSONObject(i);
			String LTBpositionStr = jsonObject.getString("init_date") + jsonObject.getString("business_time");
			if (ValueUtil.getLong(position_str) < ValueUtil.getLong(LTBpositionStr)) {
				ltbDynamicList.setSendercomp_id(jsonObject.getString("sendercomp_id"));
				ltbDynamicList.setAdviser_id(jsonObject.getString("adviser_id"));
				ltbDynamicList.setCombi_code(jsonObject.getString("combi_code"));
				ltbDynamicList.setCombi_name(jsonObject.getString("combi_name"));
				// ltbDynamicList.setClient_name(jsonObject.getString("client_name"));
				ltbDynamicList.setInit_date(jsonObject.getString("init_date"));
				ltbDynamicList.setBusiness_time(jsonObject.getString("business_time"));
				ltbDynamicList.setUser_token(LTB_user_token);
				ltbDynamicList.setPosition_str(LTBpositionStr);
				lists.add(ltbDynamicList);
			}
			if (num > request_num) {
				break;
			}
			num++;
		}
		return lists;
	}

	public List<DynamicListData> getRealtimeQryByT2(List<DynamicListData> dynamicListDatas, String company_id) {
		List<DynamicListData> lists = new ArrayList<>();
		Map<String, Object> params = new HashMap<>();

		params.put("op_branch_no", ValueUtil.getString(LTBInfo.op_branch_no));
		params.put("op_entrust_way", LTBInfo.op_entrust_way);
		params.put("op_station", LTBInfo.op_station);
		params.put("sendercomp_id", company_id);
		params.put("branch_no", LTBInfo.branch_no);
		params.put("en_entrust_status", "7,8");
		params.put("company_id", "91000");
		params.put("position_str", "");
		List<String> user_ids = new ArrayList<>();
		for (DynamicListData dynamicListData : dynamicListDatas) {
			if (ValueUtil.getString(DynamicType.DYNAMIC.getVal()).equals(dynamicListData.getDynamic_type())) {
				Map<String, Object> param = new HashMap<>();
				param.put("user_token", dynamicListData.getLtb_user_token());
				param.put("combi_code", dynamicListData.getCombi_code());
				params.put("request_num", "2");
				String url = ConfigUtil.get("ltb.realtime.qry.url", "http://10.139.55.105/trans/")
						+ "combine_basic_info_qry";
				String LTB_combine_basic_info = HttpsUtil.doHttpsPost(url, param);
				if (LTB_combine_basic_info == null || getLtbResultErrorInfo(LTB_combine_basic_info)) {
					return null;
				}
				JSONArray jsonArray = getJsonArrayFormStr(LTB_combine_basic_info);
				String busin_account = jsonArray.getJSONObject(0).getString("busin_account");
				String user_id = jsonArray.getJSONObject(0).getString("user_id");
				String businsys_no = jsonArray.getJSONObject(0).getString("businsys_no");
				params.put("user_token", dynamicListData.getLtb_user_token());
				params.put("user_id", user_id);
				params.put("busin_account", busin_account);
				params.put("businsys_no", businsys_no);
				url = ConfigUtil.get("ltb.realtime.qry.url", "http://10.139.55.105/trans/") + "comentrust_detail_qry";
				String LTB_realtime_qry = HttpsUtil.doHttpsPost(url, params);
				dynamicListData.setRealtime_qry(LTB_realtime_qry);
				dynamicListData.setUser_id(user_id);
				user_ids.add(user_id);
				/* 设置是否签约 */
				setSignStatus(dynamicListData, params);
			}
			lists.add(dynamicListData);
		}
		getAvatarFormUc(lists, StringUtils.join(user_ids, ","));
		return lists;
	}

	public static String genSign(String source, String signKey) {
		Map<String, String> map = new HashMap<>();
		map.put("source", source);
		map.put("signKey", signKey);
		String checkSign = MD5Util.encrypt(map.toString());
		return checkSign;
	}

	private JSONArray getJsonArrayFormStr(String jsonStr) {
		JSONObject obj1 = JSON.parseObject(jsonStr);
		String str1 = obj1.getString("data");
		JSONArray arr1 = JSON.parseArray(str1);
		String str2 = arr1.getString(0);
		JSONObject obj2 = JSON.parseObject(str2);
		String data_list = obj2.getString("data_list");
		return JSONUtil.toJsonArray(data_list);
	}

	private boolean getLtbResultErrorInfo(String jsonStr) {
		JSONObject obj1 = JSON.parseObject(jsonStr);
		String str1 = obj1.getString("data");
		JSONArray arr1 = JSON.parseArray(str1);
		String str2 = arr1.getString(0);
		JSONObject obj2 = JSON.parseObject(str2);
		String error_no = obj2.getString("error_no");
		if (0 != ValueUtil.getInt(error_no)) {
			return true;
		}
		return false;
	}

	private String getLTBToken(String user_token, String company_id) {
		if (!ValueUtil.isEmpty(user_token)) {
			LTBRedirectLoginTokenForm param = new LTBRedirectLoginTokenForm();
			param.setAuthorization_token(user_token);
			LTBTokenBackVo ltbTokenBackVo = ltbService.redirectLTBUserToken(param);
			return ltbTokenBackVo.getUser_token();
		} else {
			FuncLTBTokenForm param = new FuncLTBTokenForm();
			param.setCompany_id(company_id);
			param.setSendercomp_id(company_id);
			param.setOp_branch_no(ValueUtil.getString(LTBInfo.op_branch_no));
			param.setBranch_no(ValueUtil.getString(LTBInfo.branch_no));
			param.setOp_entrust_way(LTBInfo.op_entrust_way);
			param.setOp_station(LTBInfo.op_station);
			LTBTokenBackVo ltbTokenBackVo = ltbService.funcGetLTBUserToken(param);
			return ltbTokenBackVo.getUser_token();
		}
	}

	private void getAvatarFormUc(List<DynamicListData> lists, String user_ids) {
		Map<String, Object> params = new HashMap<>();
		params.put("user_ids", user_ids);
		params.put("source", "cloudbroker_1");
		params.put("sign", genSign("cloudbroker_1", "B17B4A0854FCEDF370380F3FD3FBA56C"));
		String url = ConfigUtil.get("uc.users.query.url", "http://10.253.102.179:8022") + "/func_users_query";
		String avatarList = SSLHttpUtil.doPostJson(url, JSON.toJSONString(params));
		JSONObject obj1 = JSON.parseObject(avatarList);
		String str1 = obj1.getString("data_list");
		JSONArray jsonArray = JSONUtil.toJsonArray(str1);
		Map<String, AdviserInfoVo> map = new HashMap<>();
		for (int i = 0; i < jsonArray.size(); i++) {
			com.alibaba.fastjson.JSONObject jsonObject = jsonArray.getJSONObject(i);
			AdviserInfoVo adviserInfoVo = new AdviserInfoVo();
			adviserInfoVo.setAlias_name(jsonObject.getString("alias_name"));
			adviserInfoVo.setAvatar(jsonObject.getString("avatar"));
			map.put(jsonObject.getString("user_id"), adviserInfoVo);
		}
		for (int i = 0; i < lists.size(); i++) {
			String userId = lists.get(i).getUser_id();
			AdviserInfoVo adviserInfoVo = map.get(userId);
			if (adviserInfoVo != null) {
				lists.get(i).setClient_name(adviserInfoVo.getAlias_name());
				lists.get(i).setBroker_avatar(adviserInfoVo.getAvatar());
			}
		}
	}

	private void setSignStatus(DynamicListData dynamicListData, Map<String, Object> params) {
		params.put("request_num", "50");
		String url = ConfigUtil.get("ltb.realtime.qry.url", "http://10.139.55.105/trans/") + "comuser_sign_qry";
		String signQryStr = HttpsUtil.doHttpsPost(url, params);
		JSONArray signQryList = getJsonArrayFormStr(signQryStr);
		Integer sign = 0;
		Long brokerId = dynamicListData.getBroker_id();
		for (Object object : signQryList) {
			JSONObject temp = (JSONObject) object;
			Long adviserId = ValueUtil.getLong(temp.getLong("adviser_id"), 0);
			if (brokerId.equals(adviserId)) {
				sign = 1;
				break;
			}
		}
		dynamicListData.setSign(sign);
	}
}
